package com.dy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dy.exception.NotFoundException;
import com.dy.mapper.DyBlogMapper;
import com.dy.mapper.DyBlogTagMapper;
import com.dy.mapper.DyTagMapper;
import com.dy.mapper.DyTypeMapper;
import com.dy.pojo.dto.DyBlogByConDTO;
import com.dy.pojo.dto.DyBlogDTO;
import com.dy.pojo.entity.DyBlogEntity;
import com.dy.pojo.entity.DyBlogTagEntity;
import com.dy.pojo.entity.DyTagEntity;
import com.dy.pojo.entity.DyTypeEntity;
import com.dy.pojo.vo.DyBlogTagTypeVo;
import com.dy.pojo.vo.DyBlogVO;
import com.dy.pojo.vo.DyShowTypeVO;
import com.dy.service.DyBlogService;
import com.dy.service.DyTagService;
import com.dy.utils.MarkdownUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName:
 * @Author: 仄言in
 * @Desctiption: (描述)
 * @Date: 2021/6/8 14:56
 * @Version 1.0 (版本)
 */
@Service
public class DyBlogServiceImpl extends ServiceImpl<DyBlogMapper, DyBlogEntity> implements DyBlogService {
    @Autowired
    private DyBlogMapper dyBlogMapper;
    @Autowired
    private DyTypeMapper dyTypeMapper;
    @Autowired
    private DyBlogTagMapper dyBlogTagMapper;
    @Transactional
    @Override
    public Page<DyBlogEntity> queryBolg(Integer page, Integer pageSize) {
        Page page1 = new Page(page, pageSize);
        Page<DyBlogEntity> selectPage = dyBlogMapper.selectPage(page1, null);
        List<DyBlogEntity> selectPageRecords = selectPage.getRecords();
        List selectBlogs = new ArrayList<>();
        for (int i = 0; i < selectPageRecords.size(); i++) {
            DyBlogVO dyBlogVO = new DyBlogVO();
            List<DyTypeEntity> selectList = dyTypeMapper.selectList(new QueryWrapper<DyTypeEntity>().eq("code", selectPageRecords.get(i).getTypeCode()));
            dyBlogVO.setId(selectPageRecords.get(i).getId())
                    .setTitle(selectPageRecords.get(i).getTitle())
                    .setTypeName(selectList.get(0).getName())
                    .setPublished(selectPageRecords.get(i).getPublished() ? "发布": "保存")
                    .setRecommend(selectPageRecords.get(i).getRecommend() ? "是" : "否")
                    .setCreateTime(selectPageRecords.get(i).getCreateTime())
                    .setUpdateTime(selectPageRecords.get(i).getUpdateTime());
            selectBlogs.add(dyBlogVO);
        }
        selectPage.setRecords(selectBlogs);
        return selectPage;
    }

    @Override
    public void deleteBlog(Long id) {
        dyBlogMapper.deleteById(id);
    }

    @Override
    public List<DyBlogEntity> recycleBlog() {
        return dyBlogMapper.recycleBlog();
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void taskDeleteBlog() {
        dyBlogMapper.taskDeleteBlog();
    }

    @Override
    public void updateDeleted(Long id) {
        dyBlogMapper.updateDeleted(id);
    }

    @Override
    public void updateBlog(DyBlogDTO dyBlogDTO) {
        dyBlogTagMapper.delete(new QueryWrapper<DyBlogTagEntity>().eq("blog_code",dyBlogDTO.getCode()));
        List<String> list= Arrays.asList(dyBlogDTO.getTagCode().split(","));
        List<String> taglist =new ArrayList<String>(list);
        for (int i = 0; i < taglist.size(); i++) {
            DyBlogTagEntity dyBlogTagEntity = new DyBlogTagEntity();
            dyBlogTagEntity.setBlogCode(dyBlogDTO.getCode()).setTagCode(taglist.get(i));
            dyBlogTagMapper.insert(dyBlogTagEntity);
        }

        DyBlogEntity entity = dyBlogMapper.selectById(dyBlogDTO.getId());
        entity.setCode(dyBlogDTO.getCode())
                .setTitle(dyBlogDTO.getTitle())
                .setTypeCode(dyBlogDTO.getTypeCode())
                .setContent(dyBlogDTO.getContent())
                .setFirstPicture(dyBlogDTO.getFirstPicture())
                .setFlag(dyBlogDTO.getFlag())
                .setAppreciation(dyBlogDTO.getAppreciation())
                .setShareStatement(dyBlogDTO.getShareStatement())
                .setCommentabled(dyBlogDTO.getCommentabled())
                .setPublished(dyBlogDTO.getPublished())
                .setRecommend(dyBlogDTO.getRecommend())
                .setDescription(dyBlogDTO.getDescription())
                .setId(dyBlogDTO.getId());
        dyBlogMapper.update(entity,new UpdateWrapper<DyBlogEntity>().eq("id",entity.getId()));
    }

    @Override
    public void create(DyBlogDTO dyBlogDTO,String code) {
        String bcode="B"+System.currentTimeMillis();
        DyBlogEntity dyBlogEntity = new DyBlogEntity();
        dyBlogEntity.setCode(bcode)
                .setTitle(dyBlogDTO.getTitle())
                .setUserCode(code)
                .setTypeCode(dyBlogDTO.getTypeCode())
                .setContent(dyBlogDTO.getContent())
                .setFirstPicture(dyBlogDTO.getFirstPicture())
                .setFlag(dyBlogDTO.getFlag())
                .setViews(dyBlogDTO.getViews())
                .setAppreciation(dyBlogDTO.getAppreciation())
                .setShareStatement(dyBlogDTO.getShareStatement())
                .setCommentabled(dyBlogDTO.getCommentabled())
                .setPublished(dyBlogDTO.getPublished())
                .setRecommend(dyBlogDTO.getRecommend())
                .setDescription(dyBlogDTO.getDescription());
        dyBlogMapper.insert(dyBlogEntity);
        List<String> list= Arrays.asList(dyBlogDTO.getTagCode().split(","));
        List<String> taglist =new ArrayList<String>(list);
        for (int i = 0; i < taglist.size(); i++) {
            DyBlogTagEntity dyBlogTagEntity = new DyBlogTagEntity();
            dyBlogTagEntity.setBlogCode(dyBlogEntity.getCode()).setTagCode(taglist.get(i));
            dyBlogTagMapper.insert(dyBlogTagEntity);

        }
    }

    @Override
    public DyBlogTagTypeVo selectBlogById(Long id) {
        DyBlogTagTypeVo dyBlogTagTypeVo = new DyBlogTagTypeVo();
        DyBlogEntity selectOne = dyBlogMapper.selectOne(new QueryWrapper<DyBlogEntity>().eq("id", id));
        DyTypeEntity selectTypeOne = dyTypeMapper.selectOne(new QueryWrapper<DyTypeEntity>().eq("code", selectOne.getTypeCode()));
        dyBlogTagTypeVo.setTitle(selectOne.getTitle())
                .setId(selectOne.getId())
                .setCode(selectOne.getCode())
                .setFlag(selectOne.getFlag())
                .setContent(selectOne.getContent())
                .setTypeName(selectTypeOne.getName())
                .setFirstPicture(selectOne.getFirstPicture())
                .setDescription(selectOne.getDescription());
        return dyBlogTagTypeVo;
    }

    @Override
    public List<DyBlogVO> selectBlogByCon(DyBlogByConDTO dyBlogByConDTO) {
        System.out.println("dyBlogByConDTO.toString() = " + dyBlogByConDTO.toString());
        if (dyBlogByConDTO.getTitle() != "" && dyBlogByConDTO.getRecommend() == null && dyBlogByConDTO.getTypeName()=="") {
            List<DyBlogVO> list5 = dyBlogMapper.selectBlogByCon5(dyBlogByConDTO);
            return list5;
        }
        if (dyBlogByConDTO.getTitle().equals("") && dyBlogByConDTO.getRecommend() != null && dyBlogByConDTO.getTypeName().equals("")) {
            List<DyBlogVO> list7 = dyBlogMapper.selectBlogByCon7(dyBlogByConDTO);
            return list7;
        }
        if (dyBlogByConDTO.getTitle() == "" && dyBlogByConDTO.getRecommend() == null && dyBlogByConDTO.getTypeName()!="") {
            List<DyBlogVO> list6 = dyBlogMapper.selectBlogByCon6(dyBlogByConDTO);
            return list6;
        }
        if (dyBlogByConDTO.getTitle() != "" && dyBlogByConDTO.getRecommend() != null && dyBlogByConDTO.getTypeName()=="") {
            List<DyBlogVO> list3 = dyBlogMapper.selectBlogByCon3(dyBlogByConDTO);
            return list3;
        }
        if (dyBlogByConDTO.getTitle() != "" && dyBlogByConDTO.getRecommend() == null && dyBlogByConDTO.getTypeName()!="") {
            List<DyBlogVO> list2 = dyBlogMapper.selectBlogByCon2(dyBlogByConDTO);
            return list2;
        }
        if (dyBlogByConDTO.getTitle() == "" && dyBlogByConDTO.getRecommend() != null && dyBlogByConDTO.getTypeName()!="") {
            List<DyBlogVO> list4 = dyBlogMapper.selectBlogByCon4(dyBlogByConDTO);
            return list4;
        }
        List<DyBlogVO> list1 = dyBlogMapper.selectBlogByCon1(dyBlogByConDTO);
        return list1;
    }

    @Override
    public Page<DyBlogEntity> selectlist(Integer page, Integer pageSize) {
        Page page1 = new Page(page,pageSize);
        Page<DyBlogEntity> selectPage = dyBlogMapper.selectPage(page1, null);
        return selectPage;
    }

    @Override
    public List<DyShowTypeVO> queryBy6() {
        List<String> lists = dyBlogMapper.queryBy6();
        List<DyShowTypeVO> typelist = new ArrayList<DyShowTypeVO>();
        for (int i = 0; i < lists.size(); i++) {
            DyTypeEntity selectOne = dyTypeMapper.selectOne(new QueryWrapper<DyTypeEntity>().eq("code", lists.get(i)));
            List<DyBlogEntity> selectList = dyBlogMapper.selectList(new QueryWrapper<DyBlogEntity>().eq("type_code", lists.get(i)));
            DyShowTypeVO dyShowTypeVO = new DyShowTypeVO();
            dyShowTypeVO.setName(selectOne.getName());
            dyShowTypeVO.setDyblogs(selectList);
            typelist.add(dyShowTypeVO);
        }
        return typelist;
    }

    @Override
    public List<DyBlogEntity> selectNewstBlog() {
        return dyBlogMapper.selectNewstBlog();
    }

    @Override
    public Page queryBySearch(Integer page, Integer pageSize, String query) {
        Page<DyBlogEntity> page1 = new Page<>(page,pageSize);
        Page<DyBlogEntity> selectPage = dyBlogMapper.selectPage(page1,new QueryWrapper<DyBlogEntity>().like("title",query).or().like("content",query));
        return selectPage;
    }

    @Override
    public DyBlogEntity selectShowBlogById(Long id) {
        DyBlogEntity selectOne = dyBlogMapper.selectOne(new QueryWrapper<DyBlogEntity>().eq("id", id));
        return selectOne;
    }

    @Override
    public DyBlogEntity byBlogs(Long id) {
        DyBlogEntity dyBlogEntity = dyBlogMapper.selectOne(new QueryWrapper<DyBlogEntity>().eq("id", id));
        if (dyBlogEntity == null) {
            throw new NotFoundException("该博客不存在");
        }
        DyBlogEntity blogEntity = new DyBlogEntity();
        //把dyBlogEntity的值赋值到blogEntity
        BeanUtils.copyProperties(dyBlogEntity,blogEntity);
        String content= blogEntity.getContent();
        blogEntity.setContent(MarkdownUtils.markdownToHtmlExtensions(content));
        return blogEntity;
    }

    @Override
    public Page<DyBlogEntity> queryBy6Type(Integer page, Integer pageSize) {
        Page<DyBlogEntity> page1 = new Page<DyBlogEntity>(page,pageSize);
        List<String> by6 = dyBlogMapper.queryBy6();
        Page<DyBlogEntity> selectPage = dyBlogMapper.selectPage(page1, new QueryWrapper<DyBlogEntity>().eq("type_code", by6.get(0)));
        return selectPage;
    }

    @Override
    public Page<DyBlogEntity> selectBlogByName(String name) {
        DyTypeEntity selectOne = dyTypeMapper.selectOne(new QueryWrapper<DyTypeEntity>().eq("name", name));
        return null;
    }
}
